package com.jokerpan.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.PaginationBuilder;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.Dictionary;
import com.jokerpan.project.entity.Users;
import com.jokerpan.project.entity.UsersRole;
import com.jokerpan.project.mapper.DictionaryMapper;
import com.jokerpan.project.mapper.RolesMapper;
import com.jokerpan.project.service.DictionaryService;
import com.jokerpan.project.service.RolesService;
import com.jokerpan.project.util.UserTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jokerpan
 */
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {
    @Resource
    DictionaryMapper dictionaryMapper;
    @Resource
    UserTool userTool;


    @Override
    public Map<String, Object> queryList(Dictionary dictionary) {
        Page<Object> page = PageHelper.startPage(dictionary.getCurrentPage(), dictionary.getPageSize());
        List<Dictionary> resultList = this.dictionaryMapper.queryList(dictionary);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), dictionary.getCurrentPage(), dictionary.getPageSize());
    }

    @Override
    public Dictionary queryByCode(String code) {
        return dictionaryMapper.queryByCode(code);
    }

    @Override
    public Boolean add(Dictionary dictionary) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Users users = userTool.getCurrentUser();
        dictionary.setCreateTime(format.format(new Date()));
        dictionary.setUserId(users.getId());
        return dictionaryMapper.add(dictionary) > 0;
    }

    @Override
    public ActionResult update(Dictionary dictionary) {
        if (dictionaryMapper.update(dictionary) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @Override
    public ActionResult deleteById(Integer id) {
        if (dictionaryMapper.deleteById(id) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }
}
